package com.atguigu.tingshu.account.receiver;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.availability.AvailabilityChangeEvent;
import org.springframework.stereotype.Component;


/**
 * @date: 2025/8/13 14:43
 * @author: yz
 * @version: 1.0
 */
@Component
@Slf4j
public class AccountReceiver {


    @Autowired
    private UserAccountService userAccountService;
    /**
     * 初始化用户账户
     * @param userId
     * @param message
     * @param channel
     */
    @RabbitListener(bindings = @QueueBinding(
            value =@Queue(value = MqConst.QUEUE_USER_REGISTER,durable ="true" ,autoDelete = "false"),
            exchange =@Exchange(value = MqConst.EXCHANGE_USER),
            key = {MqConst.ROUTING_USER_REGISTER}
    ))
    @SneakyThrows
    public void initUserAccount(Long userId, Message message, Channel channel){


        try {
            System.out.println("消息内容："+userId);
            if(userId!=null){
                userAccountService.initUserAccount(userId);
            }

            //确认消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            log.info("账户初始化成功。。。。。用户ID："+userId);
        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            log.error("账户初始化异常。。。用户ID："+userId);
        }



    }







}
